﻿namespace csharp基础;

public class array
{
    
    //数组声明
    //初始化
    //数据类型
    public static void arrayTest()
    {   
        //三种声明,两种初始化
        int[] array = new int[10];
        
        int[] array1 = new int[10] {1,2,3,4,5,6,7,8,9,10};;
        int[] array2 = new int[]{1,2,3,4,5,6,7,8,9,10};
        
        int[] array3 = {1,2,3,4,5,6,7,8,9,10};
     
        int[] arrayA = new int[]{1,2,3,4};
        int[] array100 = new int[100];
        int[] arrayB = new int[5];
        
        //地址搬家
        for (int i = 0; i < arrayA.Length; i++)
        {
            arrayB[i] = arrayA[i]*2;
            //Console.WriteLine("a["+i+"]"+"="+arrayB[i]);
            Console.WriteLine("arrayB="+arrayB[i]);
        }
        

    }
    //二维数组
    public static void arrayTest2()
    {
        Console.WriteLine("二维数组");
        //声明
        int[,] array0 = new int[3, 3];
        
        int[,] array1 = new int[3, 3]
        {
            { 1, 2, 3 },
            { 4, 5, 6 },
            { 7, 8, 9 },
        };
        int[,] array2 = new int[,]
        {
            { 1, 2, 3 },
            { 4, 5, 6 },
            { 7, 8, 9 },
        };
        
        int[,] array3 = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 },
        };
        
        int[,] array4 = { {1,2,2 }, {3,4,4} };
        //行
        Console.WriteLine(array4.GetLength(0));
        //列
        Console.WriteLine(array4.GetLength(1));
        // m行n列
        array4[1, 2] = 99;
        Console.WriteLine(array4[1,2]);
        //循环遍历
        /*for (int i = 0; i < 2; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                Console.WriteLine(array4[i,j]);
            }
        }*/

        #region 地址搬家
        int[,] array5 = new int[3,3];
        Console.WriteLine("array5:");
        for (int i = 0; i < array4.GetLength(0); i++)
        {
            for (int j = 0; j <array4.GetLength(1); j++)
            {
                array5[i,j]=array4[i,j];
                //这里我写了一个 ' '出现了问题.自动转化asii码 按int相加了.
                //解决办法写
                Console.Write($"{array5[i,j]}\t"); 
                // 直接格式化为字符串，避免类型转换问题 
                //Console.Write(array5[i,j]+" ");
                
            }
            Console.WriteLine();
        }
        

        #endregion
        
        /*foreach (var VARIABLE in array5)
        {   
            Console.WriteLine(VARIABLE);
        }*/
        int[,] array6 = new int[100,100];
        int a = 1;
        for (int i = 0; i < 100; i++)
        {   
            for (int j = 0; j < 100; j++)
            {   
                
                array6[i, j] = a++;
                Console.Write($"{array6[i, j]}\t");
            }
            Console.WriteLine();
        }
        
    }

    //交错数组,列不确定多少列
    public static void arrayTest3()
    {
        //初始化
        int[][] aray0;
        int[][] array1 = new int[3][];
        int[][] array2 = new int[3][]{new int[]{1,2,3},new int[]{3},new int[]{4,5}};
        int[][] array3= new int[][]{new int[]{1,2,3},new int[]{3},new int[]{4,5}};
        int[][] array4= {new int[]{1,2,3},new int[]{2,2,2},new int[]{4}};
        
        //使用
        
        //行
        Console.WriteLine(array4.GetLength(0));
        //获得某一列的长度
        Console.WriteLine(array4[2].Length);
        
        //foreach
        for (int i = 0; i < array4.GetLength(0); i++)
        {
            for (int j = 0; j < array4[i].Length; j++)
            {
                Console.Write(array4[i][j]);
            }
            Console.WriteLine();
        }
        //ai称之为锯齿数组
        /*foreach (int[] row in array4)  // 外层遍历每一行（子数组）
        {
            foreach (int element in row) // 内层遍历当前行的元素 
            {
                Console.Write(element + " ");
            }
        }*/
    }

    /*static void Main()
    {
        //arrayTest();
        //arrayTest2();
        arrayTest3();

    }*/
}